﻿using System;
using PTiRouZ.Net.SilverLight.Api;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;

namespace PTiRouZ.Net.SilverLight.Api.HelloTipi
{

    /*
     * [{"name":"rousee","title":"Famille Rous\u00e9e Ricquebourg","role":"user","read_only":0,"image_small":"http:\/\/img.hellotipi.com\/gallery\/site\/587988_440a052f49.jpg","new_public_msg":3}]
     * */
    /// <summary>Objet correspondant à la liste des site de famille</summary>
    /// <see cref="my_sites"></see>
    [DataContract]
    public class my_sites
    {
        /// <summary>nom du site de famille</summary>
        [DataMember]
        public string name { get; set; }

        /// <summary>titre du site de famille</summary>
        [DataMember]
        public string title { get; set; }

        /// <summary>rôle de l'utilisateur pour ce site: "webmaster" ou "user"</summary>
        [DataMember]
        public string role { get; set; }

        /// <summary>à true si le site es restreint (plus de credit)</summary>
        [DataMember]
        public bool read_only { get; set; }

        /// <summary>url de la vignette du site</summary>
        [DataMember]
        public string image_small { get; set; }

        /// <summary>Nombre de nouveau message</summary>
        [DataMember]
        public int new_public_msg { get; set; }

    }

    public delegate void MySitesEventHandler(object sender, MySitesEventArgs e);

    public class MySitesEventArgs : EventArgs
    {
        public MySitesEventArgs(ObservableCollection<my_sites> ms)
        {
            _result = ms;
        }

        private ObservableCollection<my_sites> _result;
        public ObservableCollection<my_sites> result { get { return _result; } }
    }

    public partial class apitxt : api
    {
        public event MySitesEventHandler MySitesCompleted;

        #region my_sites
        /// <summary>Permet de récupérer la liste des sites de famille de l'utilisateur.</summary>
        /// <returns>Retourne la liste des sites de famille de l'utilisateur identifié au format json </returns>
        /// <example>http://apitxt.hellotipi.com/account/my_sites</example>
        public void my_sites()
        {
            base.APICallCompleted += new APICallCompletedEventHandler(MySitesCallCompleted);
            GetUrlApi("account/my_sites.json");
        }

        void MySitesCallCompleted(object sender, ApiOpenReadCompletedEventArgs e)
        {
            try
            {
                MySitesCompleted.Invoke(sender, new MySitesEventArgs(serializeTools.Deserialize<ObservableCollection<my_sites>>(e.Result)));
            }
            finally
            {
                base.APICallCompleted -= new APICallCompletedEventHandler(MySitesCallCompleted);
            }
        }
        #endregion
    }
}
